const { graphql, buildSchema } = require("graphql")

const mockDb = {
  1: {
    id: 1,
    avatar: "https://www.baidu.com/s?wd=%E5%8A%B3%E5%8A%A8%E8%8A%82&sa=ire_dl_gh_logo&rsv_dl=igh_logo_pc",
    name: 'joah1',
    isTop: true,
    content: "fuck"
  },
  2: {
    id: 2,
    avatar: "https://www.baidu.com/s?wd=%E5%8A%B3%E5%8A%A8%E8%8A%82&sa=ire_dl_gh_logo&rsv_dl=igh_logo_pc",
    name: 'joah2',
    isTop: true,
    content: "fuck"
  },
  3: {
    id: 3,
    avatar: "https://www.baidu.com/s?wd=%E5%8A%B3%E5%8A%A8%E8%8A%82&sa=ire_dl_gh_logo&rsv_dl=igh_logo_pc",
    name: 'joah3',
    isTop: true,
    content: "fuck"
  },
  4: {
    id: 4,
    avatar: "https://www.baidu.com/s?wd=%E5%8A%B3%E5%8A%A8%E8%8A%82&sa=ire_dl_gh_logo&rsv_dl=igh_logo_pc",
    name: 'joah4',
    isTop: true,
    content: "fuck"
  }
}

// type => Query => 查询 => get 请求
// type => Mutation => 修改 => post 请求

const schema = buildSchema(`
  type Comment {
    id: Int
    avatar: String
    name: String
    isTop: Boolean
    content: String
  }
  type Query {
    comment: [Comment]
  }

  type Mutation {
    modify(id: Int): Int
  }
`)

schema.getQueryType().getFields().comment.resolve = () => {
  // return [{
  //   id: 1,
  //   avatar: "https://www.baidu.com/s?wd=%E5%8A%B3%E5%8A%A8%E8%8A%82&sa=ire_dl_gh_logo&rsv_dl=igh_logo_pc",
  //   name: 'joah',
  //   isTop: true,
  //   content: "fuck"
  // }]
  return Object.keys(mockDb).map((key) => {
    return mockDb[key]
  })
}
console.log(schema.getQueryType().getFields())

schema.getQueryType().getFields().modify.resolve = (...args) => {
  console.log(args)
  let id = 1;
  mockDb[id].content = "fuck you"
  return mockDb[id].content
}

module.exports = schema

// const root = {
//   hello: () => {
//     return 'hello world'
//   }
// }


// const query = (query) => {
//   return graphql(schema, query, root).then((res) => {
//     return res
//   })
// }

// module.exports = query;